/****************************************************************
 * Filename: busDemoTest.c
 * -------------------------------
 * Copyright(C),
 * Author: zhenquan.qiu
 * Version: V1.0.0
 * Last modified: 01/03 2017 17:10
 * Description:
 *
 * Change Log:
 * NO.	Author		    Date		Modified
 * 00	zhenquan.qiu	01/03 2017
 ****************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>

#define DRVDEMO_MAGIC 'K'
#define DRVDEMO_MEM_CLEAR   _IO (DRVDEMO_MAGIC, 0)

#define DRVDEMO_MEM_SET     _IO (DRVDEMO_MAGIC, 1)

#define DRVDEMO_MEM_READ    _IOR(DRVDEMO_MAGIC, 2, int)
#define DRVDEMO_MEM_write   _IOW(DRVDEMO_MAGIC, 2, int)

int main(int argc, char **agrv)
{
    int fd;
    int ret;
    char buf[20];

    fd = open("/dev/busdemo", O_RDWR);
    printf("fd: %d \n", fd);

    memset(buf, 0, sizeof(buf));
    strcpy(buf,"123");
    ret = write(fd, &buf, 3);
    printf("write ret: %d buf: %s \n", ret, buf);

    ret = lseek(fd, 0, SEEK_SET);

    memset(buf, 0, sizeof(buf));
    ret = read(fd, &buf, 3);
    printf("read ret: %d, buf: %s \n", ret, buf);

    ret = ioctl(fd, DRVDEMO_MEM_CLEAR, 0);

    ret = lseek(fd, 0, SEEK_SET);
    memset(buf, 0, sizeof(buf));
    ret = read(fd, &buf, 3);
    printf("read ret: %d, buf: %s \n", ret, buf);

    ret = ioctl(fd, DRVDEMO_MEM_SET, 3);

    ret = lseek(fd, 0, SEEK_SET);
    memset(buf, 0, sizeof(buf));
    ret = read(fd, &buf, 3);
    printf("read ret: %d, buf: %s \n", ret, buf);

    close(fd);
    return 0;
}
